package com.huatai.datadevelop.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.common.api.R;
import com.huatai.datadevelop.entity.ApiSubscreptionEntity;
import com.huatai.datadevelop.mapper.ApiSubscreptionMapper;
import com.huatai.datadevelop.utils.Query;
import com.huatai.datadevelop.websocket.RealTimeWebSocket;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author hecai
 * @description: TODO
 * @date 2023/2/13 15:19
 * @Version 1.0
 */
@Service
public class RealTimeService extends ServiceImpl<ApiSubscreptionMapper, ApiSubscreptionEntity> {

    @Autowired
    private RealTimeWebSocket realTimeWebSocket;

    /**
     * 异步推送
     * @param data
     * @param key
     */
    @Async("asyncServiceExecutor")
    public void pushData(JSONObject data,String key) throws IOException {
        realTimeWebSocket.sendMessage(key,JSON.toJSONString(data));
    }


    /**
     * 添加前置判断 subscription是否存在
     * @param subscription
     * @return
     */
    public R getSubscription(String subscription) {
        ApiSubscreptionEntity apiSub = this.getOne(Wrappers.<ApiSubscreptionEntity>lambdaQuery()
                .eq(ApiSubscreptionEntity::getSubscription, subscription));
        return apiSub!=null
                ?R.fail("接口subscription已经存在,请另起名")
                :R.success("subscription可用");
    }

    /**
     * 更新
     * @param apiSubscreption
     * @return
     */
    public R saveOrUpdateApi(ApiSubscreptionEntity apiSubscreption) {
        boolean updateSuccess = this.saveOrUpdate(apiSubscreption, Wrappers.<ApiSubscreptionEntity>lambdaUpdate()
                .eq(ApiSubscreptionEntity::getId, apiSubscreption.getId()));
        return updateSuccess?R.success("更新成功"):R.fail("内容未更新");
    }

    /**
     * 根据ID获取
     * @param apiSubId
     * @return
     */
    public R getApi(Integer apiSubId) {
        ApiSubscreptionEntity apiSub = this.getOne(new QueryWrapper< ApiSubscreptionEntity >().eq("id", apiSubId));
        return R.data(apiSub);
    }

    /**
     * 分页获取
     * @param params
     * @return
     */
    public IPage getPage(Map< String, Object > params) {
        String name = params.get("name").toString();
        IPage< ApiSubscreptionEntity > objectPage = this.page(
                new Query<ApiSubscreptionEntity>().getPage(params),
                new QueryWrapper< ApiSubscreptionEntity >().like(StringUtils.isNotBlank(name), "name", name));
        return this.page(objectPage);
    }

    /**
     * 根据ID删除
     * @param apiSubId
     * @return
     */
    public R deleteApiSub(Integer apiSubId) {
        if (apiSubId==null || apiSubId<=0){
            return R.fail(5000,"参数异常");
        }
        boolean b = this.removeById(apiSubId);
        return R.data(b?"删除成功":"删除失败");
    }

    /**
     * 批量删除
     * @param apiSubIds
     * @return
     */
    public R deleteApiSub(List apiSubIds) {
        boolean b = this.removeByIds(apiSubIds);
        return R.data(b?"删除成功":"删除失败");
    }
}
